package cn.doit.day04

import cn.doit.day03.ScalaOrder

import scala.util.Sorting

object _04_聚合算子 {
  def main(args: Array[String]): Unit = {
    //做聚合


    val orders: List[ScalaOrder] = List(
      ScalaOrder("o001", "sn001", "g001", "c001", 1, 100),
      ScalaOrder("o001", "sn002", "g002", "c002", 1, 100),
      ScalaOrder("o002", "sn001", "g001", "c001", 1, 100),
      ScalaOrder("o002", "sn002", "g003", "c003", 1, 100),
      ScalaOrder("o003", "sn001", "g002", "c002", 1, 100),
      ScalaOrder("o003", "sn003", "g002", "c002", 1, 100),
      ScalaOrder("o002", "sn001", "g003", "c003", 1, 100),
      ScalaOrder("o001", "sn002", "g003", "c003", 1, 100)
    )
    //case class 重写了equals方法
    //    val distinct: List[ScalaOrder] = orders.distinct
    //    distinct.foreach(println)
    //划分窗口                     0   1  2  3  4  5  6
    val ints: List[Int] = List(1, 2, 3, 4, 5, 6, 7)
/*    val ints1: List[Int] = ints.slice(1, 2)
    println(ints1)//List(2)
    val ints2: List[Int] = ints.slice(3, 5)
    println(ints2)//List(4, 5)*/

    //滑动
    val iterator: Iterator[List[Int]] = ints.sliding(3,2)
    iterator.foreach(println)





    //需要你找出金额最小的订单和金额最大的订单
    /*    val order1: ScalaOrder = orders.maxBy(order => order.price * order.goods_number)
        val order2: ScalaOrder = orders.minBy(order => order.price * order.goods_number)
        println(order1)
        println(order2)*/
    //        orders.sorted
    /* val orders1: List[ScalaOrder] = orders.sortBy(order => order.price * order.goods_number)
     orders1.foreach(println)

     println("===============漂亮的贺贺分隔符===============")

     val orders2: List[ScalaOrder] = orders.sortWith((o1: ScalaOrder, o2: ScalaOrder) => o1.price * o1.goods_number < o2.price * o2.goods_number)
     orders2.foreach(println)*/


    /*    val orders2: List[ScalaOrder] = orders1.take(1)
        val orders3: List[ScalaOrder] = orders1.takeRight(1)
        println(orders2)
        println(orders3)*/


    /*    val list1: List[Int] = List(6, 7, 8, 9)

    //        val res: List[(Int, Int)] = list.zip(list1)
        //    res.foreach(println)
        val index: List[(Int, Int)] = list.zipWithIndex //叫将元素和元素对应的索引进行结合组成一个元祖返回
        index.foreach(println)*/


    /* val res: Int = list.reduce((a: Int, b: Int) => {
       a + b
     })
     println(res)

     //reduce
     //reduceLeft

     //reduceRight
     val res1: Int = list.reduceRight((a: Int, b: Int) => {
       a - b
     })//
     println(res1)//3*/

    //fold   spark里面就不一样了  ==》 并行度  3个并行度
    /*    println(list.fold(10)((a: Int, b: Int) => a - b))//-5

        //foldLeft
        //foldRight
        println(list.foldRight(10)((a:Int,b:Int)=>a-b))//-7
        //5 - 10  =-5  9 -6  8  -7*/


  }
}
